from flask import Blueprint
from flask import jsonify
from extensions import db
from models import Attendance


# 创建蓝图
get_attendance_bp = Blueprint('get_attendance', __name__)
@get_attendance_bp.route('/get_attendance', methods=['GET'])
# ----------------获取考勤数据-----------------------
def get_attendance():
    try:
        # 查询数据库中的所有考勤记录
        records = Attendance.query.all()
        
        # 将查询结果转换为字典列表
        attendance_records = []
        for record in records:
            attendance_records.append({
                'date': record.date.strftime('%Y-%m-%d'),  # 将日期格式化为字符串
                'morningReading': record.morning_absent.split(",") if record.morning_absent else [],
                'exercise': record.exercise_absent.split(",") if record.exercise_absent else [],
                'eveningRollCall': record.school_over_absent.split(",") if record.school_over_absent else [],
                'unsubmitphone': record.phone_absent.split(",") if record.phone_absent else []
            })
        # 返回 JSON 数据
        return jsonify(attendance_records)
    except Exception as e:
        print("出错信息：",e)
        # 返回错误信息
        return jsonify({
            'error': '服务器内部错误',
            'message': str(e)
        }), 500  # 500 表示服务器内部错误
        


